package mock;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;

import servlets.TerminplanerServlet;

public class TerminplanerServletTest {
	
	HttpServletRequest mockRequest;
	HttpServletResponse mockResponse;
	HttpSession mockSession;
	RequestDispatcher mockRequestDispatcher;
	
	TerminplanerServlet servlet;
	
	@Before
	public void setup() {
		
		mockRequest = Mockito.mock(HttpServletRequest.class);
		mockResponse = Mockito.mock(HttpServletResponse.class);
		mockSession = Mockito.mock(HttpSession.class);
		when(mockRequest.getSession()).thenReturn(mockSession);
		mockRequestDispatcher = Mockito.mock(RequestDispatcher.class);
		when(mockRequest.getRequestDispatcher(Matchers.anyString())).thenReturn(mockRequestDispatcher);
		
		servlet = new TerminplanerServlet();
	}

	@Test
	public void doPost() throws ServletException, IOException {
		
		servlet.doPost(mockRequest, mockResponse);

		verify(mockResponse, times(1)).sendRedirect(Matchers.matches("TerminplanerServlet"));
	}
	
	 @Test
	  public void doGet() throws ServletException, IOException {
	    
	    when(mockRequest.getParameter(Matchers.matches("token"))).thenReturn("123");
	    
	    servlet.doGet(mockRequest, mockResponse);
	    
	    verify(mockRequest, times(1)).getRequestDispatcher(Matchers.matches("/WEB-INF/jsps/terminplaner.jsp"));
	  }

}
